package s.k.gui.webtest.page;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

//import org.junit.AfterClass;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import s.k.gui.webtest.Allocator;
import s.k.gui.webtest.BrowserWindow;
import s.k.gui.webtest.WebDriverBrowserWindow;
import s.k.gui.webtest.basetools.SKBaseTools;
import s.k.gui.webtest.constvalue.*;

import s.k.gui.webtest.element.Element;
import s.k.gui.webtest.element.locator.Locator;
import s.k.gui.webtest.element.locator.LocatorChain;
import s.k.gui.webtest.element.locator.LocatorValue;
import s.k.gui.webtest.log.MyLogger;

public class Page implements LocatorValue {

	String url;
	
	private boolean selected;

	protected String comment;

	public void setComment(String comment) {

		this.comment = comment;

	}

	public String getComment(String comment) {

		return comment;
	}

	public static MyLogger logger = MyLogger.getLogger(Frame.class);

	protected List<Element> elements = new ArrayList<Element>();

	protected List<SubPage> subPages = new ArrayList<SubPage>();

	protected List<Frame> frames = new ArrayList<Frame>();

	protected List<Part> parts = new ArrayList<Part>();
	
	public Page(){
		
		SKBaseTools.configLogProperties();
		
		this.selected=true;
	}

	// @AfterClass
	public void init() {

		Field[] fields = this.getClass().getDeclaredFields();

		try {

			for (Field field : fields) {

				if (field.getModifiers() == ConstValue.nestedFatherObjectModifier) {

					continue;

				}
				Object o = field.get(this);

				if (o instanceof Element) {

					SKBaseTools.insertElementName((Element) o, field.getName());

					((Element) o).getInfoChain().addNode(this);

					((Element) o).setFullName(getClass().getName().substring(getClass().getName().lastIndexOf('.') + 1)+ field.getName());

					elements.add((Element) o);

				} else if (o instanceof SubPage) {

					SKBaseTools.insertSubPageName((SubPage) o, field.getName());

					((SubPage) o).getInfoChain().addNode(this);

					((SubPage) o).setFullName(getClass().getName().substring(getClass().getName().lastIndexOf('.') + 1)+ field.getName());

					this.subPages.add((SubPage) o);

					((SubPage) o).init();

				} else if (o instanceof Part) {

					SKBaseTools.insertPartName((Part) o, field.getName());

					((Part) o).getInfoChain().addNode(this);

					((Part) o).setFullName(getClass().getName().substring(getClass().getName().lastIndexOf('.') + 1)+ field.getName());

					this.parts.add((Part) o);

					((Part) o).init();

				} else if (o instanceof Frame) {

					SKBaseTools.insertFrameName((Frame) o, field.getName());

					((Frame) o).getInfoChain().addNode(this);

					((Frame) o).setFullName(getClass().getName().substring(
							getClass().getName().lastIndexOf('.') + 1)
							+ field.getName());

					this.frames.add((Frame) o);

					((Frame) o).init();
				}
			}

		} catch (IllegalAccessException e) {

			e.printStackTrace();

		} catch (IllegalArgumentException e) {

			e.printStackTrace();
		}
	}


	public String getBodyText() {

		logger.info("获得" + this.comment + "ҳ��bodyԴ��");

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			return ((WebDriver) currentWindow.getDriver().getEngine())
					.getPageSource();

		}
		return null;
	}

	public String getHtmlText() {

		logger.info("获得" + this.comment + "页面body的源代码");

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			return ((WebDriver) currentWindow.getDriver().getEngine())
					.getPageSource();

		}
		return null;

	}

	public String getTitle() {

		logger.info("获得" + this.comment + "所有活动页面的title");

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			return ((WebDriver) currentWindow.getDriver().getEngine())
					.getTitle();

		}
		return null;

	}

	public String getAllCookies() {

		logger.info("获得" + this.comment + "所有cookies");

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			Set<Cookie> cookies = ((WebDriver) currentWindow.getDriver()
					.getEngine()).manage().getCookies();

			Iterator<Cookie> cookie = cookies.iterator();

			String cookieStr = "";

			while (cookie.hasNext()) {

				cookieStr = cookieStr + "|" + cookie.next().getValue();

			}

			return cookieStr;

		}
		return null;
	}

	public String getCookieByName(String name) {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			return ((WebDriver) currentWindow.getDriver().getEngine()).manage()
					.getCookieNamed(name).getValue();
		}

		return null;
	}
	/**删除该指定目录下的指定名字的cookie的所有的cookie*/
	public void deleteCookie(String name, String path) {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			((WebDriver) currentWindow.getDriver().

			getEngine()).manage().deleteCookieNamed(name);

		}
	}
	/**删除所有的cookie*/
	public void deleteAllCookie() {

		BrowserWindow currentWindow = Allocator.getInstance().currentWindow;

		if (currentWindow instanceof WebDriverBrowserWindow) {

			((WebDriver) currentWindow.getDriver().

			getEngine()).manage().deleteAllCookies();

		}
	}

	@Override
	public Locator getLocator() {

		return null;
	}

	@Override
	public String getComment() {

		return null;
	}

	@Override
	public WebElement getElement() {

		return null;
	}

	@Override
	public void setElement(WebElement element) {


	}

	@Override
	public String getLocatorCurrent() {

		return null;
	}

	@Override
	public void setLocatorCurrent(String locatorCurrent) {


	}

	@Override
	public boolean isExist() {

		return false;
	}

	@Override
	public LocatorChain getInfoChain() {

		return null;
	}
	public boolean isSelected() {
		
		return selected;
	}
	public void setSelected(boolean selected) {
		
		this.selected = selected;
	}
}
